package com.xd.core.business.controller;


import com.xd.common.constant.SysConstant;
import com.xd.core.business.dto.CreditContractDto;
import com.xd.core.business.dto.CreditDto;
import com.xd.core.business.dto.CreditInsuranceDto;
import com.xd.core.business.mapper.CreditMapper;
import com.xd.core.business.service.ICreditService;
import com.xd.core.business.vo.CreditVo;
import com.xd.core.debt.dto.Review;
import com.xd.core.product.vo.ProductRepayment;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 授信表 前端控制器
 * </p>
 *
 * @author ZX
 * @since 2025-02-23
 */
@Api(tags = "授信管理")
@RestController
@RequestMapping("/core/credit")
public class CreditController {

    @Autowired
    private ICreditService iCreditService;
    @Autowired
    private CreditMapper creditMapper;


    @PreAuthorize("hasAnyAuthority('sys:Rereview')")
    @ApiOperation("编辑保单贷授信")
    @PostMapping("/updateInsuranceCredit")
    public void updateInsuranceCredit(@RequestBody CreditInsuranceDto creditDto) {
        iCreditService.updateInsuranceCredit(creditDto);
    }

    @PreAuthorize("hasAnyAuthority('sys:PolicyReview')")
    @ApiOperation("授信审核")
    @PostMapping("/updateInsuranceStatus")
    public void updateInsuranceStatus(@RequestBody Review review) {
        iCreditService.updateInsuranceStatus(review);
    }

    @ApiOperation("授信返显")
    @GetMapping("/getInsuranceIncomingAndCredit/{incomingId}")
    public CreditInsuranceDto getInsuranceIncomingAndCredit(@PathVariable Integer incomingId) {
      return  iCreditService.getInsuranceIncomingAndCredit(incomingId);
    }

    @PreAuthorize("hasAnyAuthority('sys:EditCredit')")
    @ApiOperation("编辑授信")
    @PostMapping("/updateCredit")
    public void updateCredit(@RequestBody CreditDto creditDto) {
        iCreditService.updateCredit(creditDto);
    }


    @PreAuthorize("hasAnyAuthority('sys:Incoming-EditCreditFail')")
    @ApiOperation("授信失败")
    @PostMapping("/updateCreditFail")
    public void updateCreditFail(@RequestBody CreditDto creditDto) {
        iCreditService.updateCreditFail(creditDto);
    }


    @ApiOperation("授信详情")
    @GetMapping("/getCreditDetail/{id}")
    public CreditVo getCreditDetail(@PathVariable Integer id) {
        CreditVo creditVo = creditMapper.selectCredit(id);
        // 处理还款备注信息
        creditVo.setRepaymentRemark(buildRepaymentRemark(creditVo.getRepayment()));
        return creditVo;
    }

    private List<ProductRepayment> buildRepaymentRemark(String repaymentStr) {
        if (StringUtils.isBlank(repaymentStr)) {
            return Collections.emptyList();
        }

        return Arrays.stream(repaymentStr.split(","))
                .map(this::convertToRepayment)
                .collect(Collectors.toList());
    }

    private static final Map<String, String> REPAYMENT_MAPPING = new HashMap<>();

    static {
        REPAYMENT_MAPPING.put(SysConstant.REPAY_TYPE_ZERO, "等额本金");
        REPAYMENT_MAPPING.put(SysConstant.REPAY_TYPE_ONE, "等额本息");
        REPAYMENT_MAPPING.put(SysConstant.REPAY_TYPE_TWO, "等本等息");
        REPAYMENT_MAPPING.put(SysConstant.REPAY_TYPE_THREE, "到期还本付息");
        REPAYMENT_MAPPING.put(SysConstant.REPAY_TYPE_FOUR, "先息后本");
    }

    private ProductRepayment convertToRepayment(String type) {
        String translated = REPAYMENT_MAPPING.getOrDefault(type, "未知还款类型");

        return new ProductRepayment() {{
            setRepayment(type);
            setRepaymentRemark(translated);
        }};
    }

    @PreAuthorize("hasAnyAuthority('sys:SignContract')")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("签署合同--修改授信内容")
    @PostMapping("/updateCreditAccount")
    public void updateCreditAccount(@RequestBody CreditContractDto creditContractDto) {
        iCreditService.updateCreditAccount(creditContractDto);
    }



}
